/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.dao.hibernate;

import com.click.app.dao.AsistanceControlDao;
import com.click.app.model.AsistanceControl;
import com.click.app.model.Employee;
import java.util.Date;
import java.util.List;

/**
 *
 * @author org.@le
 */
public class AsistanceControlDaoHibernate extends GenericDaoHibernate<AsistanceControl, Long> implements AsistanceControlDao {

    public AsistanceControlDaoHibernate() {
        super(AsistanceControl.class);
    }

    public List<AsistanceControl> findByEmployee(Employee employee) {
        return getHibernateTemplate().find("from AsistanceControl where employee=? order by date", employee);
    }

    public int sizeByEmployeePeriod(Employee employee, Date initDate, Date endDate) {
        Object[] objects = {employee, initDate, endDate};
        List<AsistanceControl> aCL = getHibernateTemplate().find("from AsistanceControl where employee=? and halfDay=false and date Between ? And ? ", objects);
        List<AsistanceControl> aCL1 = getHibernateTemplate().find("from AsistanceControl where employee=? and halfDay=true and date Between ? And ? ", objects);
        return aCL.size() + (aCL1.size() / 2);
    }

    public boolean sizeByHalfEmployeePeriod(Employee employee, Date initDate, Date endDate) {
        Object[] objects = {employee, initDate, endDate};
        List<AsistanceControl> aCL1 = getHibernateTemplate().find("from AsistanceControl where employee=? and halfDay=true and date Between ? And ? ", objects);
        return (aCL1.size() % 2) > 0;
    }

    public boolean dateExist(Employee employee, Date date) {
        Object[] objects = {employee, date};
        List<AsistanceControl> aCL = getHibernateTemplate().find("from AsistanceControl where employee=? and date=?", objects);
        if (aCL.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }
}
